# Author: Nic Wolfe <nic@wolfeden.ca>
# URL: http://code.google.com/p/sickbeard/
#
# This file is part of Sick Beard.
#
# Sick Beard is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Sick Beard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Sick Beard.  If not, see <http://www.gnu.org/licenses/>.



import urllib
import urllib2
import os.path
import sys
import re 


from lib.BeautifulSoup import BeautifulStoneSoup

import sickbeard
import sickbeard.classes
import sickbeard.helpers



from sickbeard.common import *
from sickbeard.logging import *



providerType = "nzb"
providerName = "TVBinz"

def isActive():
	return sickbeard.TVBINZ and sickbeard.USE_NZB

def getTVBinzURL (url):

	searchHeaders = {"Cookie": "uid=" + sickbeard.TVBINZ_UID + ";hash=" + sickbeard.TVBINZ_HASH, 'Accept-encoding': 'gzip'}
	req = urllib2.Request(url=url, headers=searchHeaders)
	
	try:
		f = urllib2.urlopen(req)
	except (urllib.ContentTooShortError, IOError) as e:
		Logger().log("Error loading TVBinz URL: " + str(sys.exc_info()) + " - " + str(e))
		return None

	result = sickbeard.helpers.getGZippedURL(f)

	return result

						
def downloadNZB (nzb):

	Logger().log("Downloading an NZB from tvbinz at " + nzb.url)

	data = getTVBinzURL(nzb.url)
	
	if data == None:
		return False
	
	fileName = os.path.join(sickbeard.NZB_DIR, nzb.extraInfo[0] + ".nzb")
	
	Logger().log("Saving to " + fileName, DEBUG)
	
	fileOut = open(fileName, "w")
	fileOut.write(data)
	fileOut.close()

	return True
	
	
def findEpisode (episode, forceQuality=None):

	if episode.status == DISCBACKLOG:
		Logger().log("TVbinz doesn't support disc backlog. Use newzbin or download it manually from TVbinz")
		return []

	Logger().log("Searching tvbinz for " + episode.prettyName())

	if forceQuality != None:
		epQuality = forceQuality
	elif episode.show.quality == BEST:
		epQuality = ANY
	else:
		epQuality = episode.show.quality
	
	if epQuality == SD:
		quality = {"priority": "sd", "wait": "9999999999999"}
	elif epQuality == HD:
		quality = {"priority": "hd", "wait": "9999999999999"}
	else:
		quality = {}
		
	sceneSearchStrings = sickbeard.helpers.makeSceneSearchString(episode)
	
	finalSearchString = "|".join([x for x in sceneSearchStrings])
	
	params = {"search": finalSearchString, "nodupes": "1", "normalize": "1", "maxage": sickbeard.USENET_RETENTION}
	params.update(quality)
	
	searchURL = "https://tvbinz.net/rss.php?" + urllib.urlencode(params)

	Logger().log("Search string: " + searchURL, DEBUG)

	data = getTVBinzURL(searchURL)
	
	if data == None:
		return []

	results = []
	
	responseSoup = BeautifulStoneSoup(data)
	items = responseSoup.findAll('item')
		
	for item in items:
		
		if item.title == None or item.link == None:
			Logger().log("The XML returned from the TVBinz RSS feed is incomplete, this result is unusable: "+data, ERROR)
			continue
		
		title = item.title.string
		url = item.link.string.replace("&amp;", "&")
		urlParams = {'i': sickbeard.TVBINZ_UID, 'h': sickbeard.TVBINZ_HASH}
		
		Logger().log("Found result " + title + " at " + url)

		result = sickbeard.classes.NZBSearchResult(episode)
		result.provider = sickbeard.common.TVBINZ
		result.url = url + "&" + urllib.urlencode(urlParams) 
		result.extraInfo = [title]
		result.quality = epQuality
		
		results.append(result)
		
	return results
    
def findAllEpisodes (epList, forceQuality=None):

    params = {"nodupes": "1", "normalize": "1", "seriesinfo": "1"}
    #params.update(quality)
	
    searchURL = "https://tvbinz.net/rss.php?" + urllib.urlencode(params)

    Logger().log("Search string: " + searchURL, DEBUG)

    data = getTVBinzURL(searchURL)
    if data == None:
        return []

    results = []

    
    responseSoup = BeautifulStoneSoup(data)
    items = responseSoup.findAll('item')
	
    for item in items:
        if item.title == None or item.link == None:
            Logger().log("The XML returned from the TVBinz RSS feed is incomplete, this result is unusable: " + data, ERROR)
            continue
        if item.seriesinfo == None:
            Logger().log(item.title.string + " : Not supported Show", DEBUG)
            continue
        for curShow in epList:
            #print curShow.tvdbid
            #print curShow.tvrid
            title = item.title.string
            #Logger().log("Compare " + title + " with " + curShow.name)
            seriesName = item.seriesinfo.seriesname.string
            if item.seriesinfo.tvrid != None:            
                tvrid = item.seriesinfo.tvrid.string
                if str(curShow.tvrid) != str(tvrid):
                    #Logger().log(seriesName + " " +  curShow.name + " " + str(tvrid) + " "  + str(curShow.tvrid))
                    continue
            elif seriesName != curShow.name:
                continue
            title = item.title.string
            strre=re.compile('720p|1080p', re.IGNORECASE)
            m=strre.search(title)
            if m:
                #Logger().log(title + " is HD")
                epQuality = HD
            else:
                #Logger().log(title + " is SD")
                epQuality = SD
                
            showQuality = curShow.quality
            
            if showQuality == ANY or showQuality == BEST:
                #Logger().log(title + " change to any download " )
                showQuality = epQuality
                
            if showQuality != epQuality:
                Logger().log(title + " not good quality type ")
                continue

            try:             
                ep = curShow.getEpisode(item.seriesinfo.seasonnum.string, item.seriesinfo.episodenum.string)
            except:
                Logger().log(title + " " + " dont know what episode this is")
                continue
            if ep.status ==  DOWNLOADED or ep.status == SNATCHED or ep.status == SKIPPED:
                Logger().log(title + " already downloaded / or skipped ", DEBUG)
                continue
            
            
            url = item.link.string.replace("&amp;", "&")
            urlParams = {'i': sickbeard.TVBINZ_UID, 'h': sickbeard.TVBINZ_HASH}
            Logger().log("Found result " + title + " at " + url)
            result = sickbeard.classes.NZBSearchResult(ep)
            result.provider = sickbeard.common.TVBINZ
            result.url = url + "&" + urllib.urlencode(urlParams)
            result.extraInfo = [title]
            result.quality = epQuality
            results.append(result)
    return results 
